package com.shunbo.convenient.controller;


import com.shunbo.convenient.service.OpinionService;
import com.shunbo.convenient.utils.ConvenientResult;
import com.shunbo.convenient.vo.Opinion;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author weizhuang
 * 意见反馈
 */
@RestController
@RequestMapping("/opinion")
public class OpinionController {


    @Resource
    private OpinionService opinionService;


    /**
     * 意见列表
     * @param opinion 意见信息
     * @return Result结果集
     */
    @RequestMapping("/list")
    public ConvenientResult findOpinionList(@RequestBody Opinion opinion){
        try {
            List<Opinion> all = opinionService.findAll(opinion);
            return ConvenientResult.build(200,"成功",all);
        }catch (Exception e){
            e.printStackTrace();
            return ConvenientResult.build(500,"系统错误",e.getStackTrace());
        }
    }

    /**
     * 添加意见
     * @param opinion 意见信息
     * @return Result结果集
     */
    @RequestMapping("/save")
    public ConvenientResult saveOpinion(@RequestBody Opinion opinion){
        try {
            Integer save = opinionService.save(opinion);
            return ConvenientResult.build(200,"成功",save);
        }catch (Exception e){
            e.printStackTrace();
            return ConvenientResult.build(500,"系统错误",e.getStackTrace());
        }
    }

    /**
     * 更新意见
     * @param opinion 意见信息
     * @return Result结果集
     */
    @RequestMapping("/update")
    public ConvenientResult updateOpinion(@RequestBody Opinion opinion){
        try {
            Integer save = opinionService.update(opinion);
            return ConvenientResult.build(200,"成功",save);
        }catch (Exception e){
            e.printStackTrace();
            return ConvenientResult.build(500,"系统错误",e.getStackTrace());
        }
    }

    /**
     * 删除意见
     * @param opinion 意见信息
     * @return Result结果集
     */
    @RequestMapping("/delete")
    public ConvenientResult deleteOpinion(@RequestBody Opinion opinion){
        try {
            Integer save = opinionService.delete(opinion.getId()+"");
            return ConvenientResult.build(200,"成功",save);
        }catch (Exception e){
            e.printStackTrace();
            return ConvenientResult.build(500,"系统错误",e.getStackTrace());
        }
    }

    /**
     * 通过id查找意见信息
     * @param opinion 意见信息
     * @return Result结果集
     */
    @RequestMapping("/findOne")
    public ConvenientResult findUserInfoById(@RequestBody Opinion opinion){
        try {
            Opinion byId = opinionService.findById(opinion.getId() + "");
            return ConvenientResult.build(200,"成功",byId);
        }catch (Exception e){
            e.printStackTrace();
            return ConvenientResult.build(500,"系统错误",e.getStackTrace());
        }
    }


}
